package com.debao;


import com.debao.common.annotation.Anonymous;
import com.debao.common.constant.Constants;
import com.debao.common.core.controller.BaseController;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.core.domain.entity.SysUser;
import com.debao.common.core.page.TableDataInfo;
import com.debao.common.utils.SecurityUtils;
import com.debao.common.wechat.WechatUtils;
import com.debao.system.domain.Circle;
import com.debao.system.domain.Interest;
import com.debao.system.service.ICircleService;
import com.debao.system.service.IInterestService;
import com.debao.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RequestMapping("/api/circle")
@RestController
@CrossOrigin
public class ApiCircle extends BaseController {

    @Resource
    private ICircleService circleService;


    @Autowired
    private ISysUserService userService;


    @Resource
    private IInterestService interestService;

    /**
     * 获取帖子列表
     *
     * @param circle
     * @return
     */
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(Circle circle) {
        Long userId = SecurityUtils.getLoginUserId();
        List<Circle> circles = circleService.selectCircleList(circle);
        if (userId != null) {
            circles.forEach(c -> c.setIsFollow(interestService.selectInterestByUserId(userId, c.getUserId())));
        }
        startPage();
        return getDataTable(circles);
    }

    /**
     * 发布帖子
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Circle circle) {
        circle.setUserId(SecurityUtils.getUserId());
        SysUser sysUser = userService.selectUserById(circle.getUserId());
        if (sysUser.getPostNumber() == 0) {
            return AjaxResult.error("您本月已动态数已达上限，请下月再发");
        }
        return toAjax(circleService.insertCircle(circle));
    }

    /**
     * 获取帖子详情
     */
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable("id") Long id) {
        return AjaxResult.success(circleService.selectCircleById(id));
    }

    /**
     * 修改帖子
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Circle circle) {
        circle.setStatus(Constants.STATUS_0);
        return toAjax(circleService.updateCircle(circle));
    }

    /**
     * 删除帖子
     */
    @PostMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        return toAjax(circleService.deleteCircleById(id));
    }

    /**
     * 发布帖子
     */
    @PostMapping("/addZan/{id}")
    public AjaxResult addZan(@PathVariable("id") Long id) {
        circleService.addZan(id);
        return AjaxResult.success();
    }

    /**
     * 关注教练
     */
    @PostMapping("/interest/{userId}")
    public AjaxResult interest(@PathVariable("userId") Long userId) {
        int row = interestService.insertInterest(new Interest().setOtherId(userId).setUserId(SecurityUtils.getUserId()));
        if (row == 1) {
            return AjaxResult.success("关注成功");
        }
        return AjaxResult.error("关注失败");
    }

    /**
     * 取消关注教练
     */
    @DeleteMapping("delInterest/{userId}")
    public AjaxResult delInterest(@PathVariable("userId") Long userId) {
        int row = interestService.deleteInterest(userId, SecurityUtils.getUserId());
        if (row == 1) {
            return AjaxResult.success("取消关注成功");
        }
        return AjaxResult.error("取消关注失败");
    }
}
